package com.alex.controller.High_Scoring_Topics;

import com.alex.controller.System.AliClientToolNoStreamJson;
import com.alex.controller.System.entity.ChatEntity;
import com.alex.sevice.ChatGptService;
import common.beans.User;
import common.util.UserUtil;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/high_scoring_topics")
@RestController
@Tag(name = "高分选题_张彤")
@SuppressWarnings("all")
public class HighScoringTopicsController {

    @Autowired
    private AliClientToolNoStreamJson aliClientToolNoStreamJson;
    @Autowired
    private ChatGptService chatGptService;


    @GetMapping("/high_scoring_topics")
    public void pubmed_search(@Parameter(name = "content", description = "要输入的内容", example = "这篇文章的课题思路是研究阿尔茨海默病（AD）中神经元细胞丧失的机制。研究人员将人类或小鼠的神经元移植到AD小鼠的大脑中。只有人类神经元显示出缠结、Gallyas银染色、颗粒囊变性（GVD）、磷酸化tau血液生物标志物和大量神经元细胞丧失。长链非编码RNA MEG3在人类神经元中被强烈上调。这种神经元特异性的长链非编码RNA在AD患者中也上调。单独表达MEG3就足以在体外诱导人类神经元的坏死程序性细胞死亡。通过下调MEG3和使用药物或基因干预受体相互作用蛋白激酶1（RIPK1）、RIPK3或混合谱系激酶结构域样蛋白（MLKL）来抑制坏死程序性细胞死亡，可以拯救移植的人类神经元的细胞丧失。这个模型为AD提供了潜在的治疗方法，并揭示了人类对AD的特定易感性。", required = true) String content, @Parameter(name = "user", description = "用户的登录 ID", example = "22", required = true) String user, HttpServletResponse response) throws IOException {

        System.out.println("用户提问的内容是:" + content);
        List<ChatEntity> list = new ArrayList<>();
        list.add(new ChatEntity("system",
                "\n请依据用户给出的数据，生成五个与科研相关的中文课题思路及其简介。请依照以下JSON格式生成字符串数据：\n" +
                        "[\n" +
                        "  {\"title\": \"${title_1}\", \"introduction\": \"${introduction_1\"}},\n" +
                        "  {\"title\": \"${title_2}\", \"introduction\": \"${introduction_2}},\n" +
                        "  {\"title\": \"${title_3}\", \"introduction\": \"${introduction_3}},\n" +
                        "  {\"title\": \"${title_4}\", \"introduction\": \"${introduction_4}},\n" +
                        "  {\"title\": \"${title_5}\", \"introduction\": \"${introduction_5}}\n" +
                        "]\n" +
                        "确保每个课题思路（`title`）具有创新性和针对性，而简介（`introduction`）则简明扼要地阐述该课题的研究目的、方法或预期成果，与所提科研方向紧密相关。"));
        list.add(new ChatEntity("user", content));
        String send = aliClientToolNoStreamJson.send(list);
        aliClientToolNoStreamJson.sendToClint(response, send);
    }


    @GetMapping("/search_keyword")
    public void search_keyword(String content, HttpServletResponse response) {
        // 需要指定response的ContentType为流式输出，且字符编码为UTF-8
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        // 禁用缓存
        response.setHeader("Cache-Control", "no-cache");
        System.out.println("用户提问的内容是:" + content);
        User user = UserUtil.getUserUnloginAccess();


        try {
            chatGptService.ai_web_chat(response, user, false, "" + content + "\n根据所提供的课题思路，生成相应的检索词串，以空格加OR连接各关键词，例如：A OR B OR C。请提供具体的课题思路关键词，直接返回拼接好的检索词，确保结果中不包含引号或其他无关符号。", 0, 0);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}
